package spring.aop.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author GY
 * @date 2019年1月19日
 * @说明: 代理类执行代理动作的辅助类
 */
public class Ih implements InvocationHandler {
    private Object target; // 被代理的对象
    private Ih(Object target) {
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("Ih.invoke begin");
        Object object = method.invoke(target, args);
        System.out.println("Ih.invoke end");
        return object;
    }
    private Object getInstance() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
    }
    public static Object getTargetProxyInstance(Object target) {
        return new Ih(target).getInstance();
    }
    public static Object getMyTargetProxyInstance(Object target) {
        return new MyIServiceProxy(new Ih(target));
    }
}
